﻿using LBCMS.BLL;
using LBCMS.EntityFrameWork;
using LBCMS.EntityFrameWork.FrameModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using static LBCMS.BLL.PredicateBuilder;

namespace LBCMS.Controllers
{
    public class AgendaController : Controller
    {
        EntityFrameWork.BLLContext db = new EntityFrameWork.BLLContext();

        public ActionResult Index()
        {
            return View();
        }

        public string getPageData(queryCondition where, int page, int limit)
        {
            return new BLL.frameHelper().getData<TW_Agenda>(where, page, limit, new OrderModelField() { IsDESC = true, PropertyName = "id" });
        }

        public ActionResult Data(string rowGuid)
        {
            //模式
            if (Request["id"] != null && Request["id"].ToString() != "")
            {
                ViewBag.Mode = "edit";
            }

            return View();
        }

        public string Delete(string ids)
        {
            string[] arrIds = ids.Split(';');
            foreach (var item in arrIds)
            {
                if (item != "")
                {
                    var data = new TW_Agenda();
                    data.id = Convert.ToInt32(item);
                    db.TW_Agenda.Attach(data);
                    db.Entry(data).State = System.Data.Entity.EntityState.Deleted;
                }
            }

            db.SaveChanges();

            return Helper.JsonHelper.toSuccessJson();
        }

        public string getData(int id)
        {
            return Helper.JsonHelper.toJsonTime(new EntityComm().GetSingleById<TW_Agenda>(id));
        }
        public string Save(TW_Agenda data)
        {
            if (data.id > 0)
            {
                db.TW_Agenda.Attach(data);
                db.Entry(data).State = System.Data.Entity.EntityState.Modified;
            }
            else
            {
                db.TW_Agenda.Add(data);
            }

            db.SaveChanges();
            return Helper.JsonHelper.toSuccessJson();
        }


        public string getActivities()
        {
            List<treeNode> list = new List<treeNode>();

            var years = db.TW_Activity.Where(p => true).GroupBy(p => new { p.year }).OrderByDescending(p => new { p.Key.year }).Select(p => p.Key.year).Take(2).ToList();
            foreach (var i in years)
            {
                treeNode tn = new treeNode() { id = i, name = i, spread = false };
                list.Add(tn);
            }
            var acts = db.TW_Activity.Where(p => years.Contains(p.year)).OrderByDescending(p => p.year).ThenBy(p => p.startTime).ThenBy(p => p.endTime).Select(p => new { p.year, p.id, p.aName }).ToList();
            foreach (var i in acts)
            {
                var tn = list.Where(p => p.id == i.year).FirstOrDefault();
                if (tn != null)
                {
                    if (tn.children == null)
                    {
                        List<treeNode> ln = new List<treeNode>();
                        ln.Add(new treeNode() { id = i.id.ToString(), name = i.aName, spread = false });
                        tn.children = ln;
                        //tn.children.Insert(0,new treeNode() { id = i.id.ToString(), name = i.year, spread = false });
                    }
                    else
                        tn.children.Add(new treeNode() { id = i.id.ToString(), name = i.aName, spread = false });
                }
            }

            return Helper.JsonHelper.toJson(list);
        }


    }
}
